#!/bin/sh

if [ ! -e $THIRD_PARTY ]; then
    echo "Creating directory for download files..."
    mkdir $THIRD_PARTY
fi
if [ ! -e $BUILDS ]; then
    echo "Creating directory for builds..."
    mkdir $BUILDS
fi

for URL in $@; do
    DO_DOWNLOAD='True'
    DO_INSTALL='True'
    BASE_URL=`dirname $URL`
    FILE=`echo $URL | sed -e "s|$BASE_URL/||"`
    if [[ `echo $FILE | sed -n '/^Python/{p;q;}'` == $FILE ]]; then
        if [[ "$BUILD_PYTHON" == 'False' ]]; then
            DO_DOWNLOAD='False'
            DO_INSTALL='False'
        fi
    fi
    if [ -f ${THIRD_PARTY}/$FILE ]; then
        DO_DOWNLOAD='False'
        echo "$FILE is already downloaded; skipping..."
    fi
    if [[ "$DO_DOWNLOAD" == 'True' ]]; then
        echo "Downloading $FILE ..."
        $DOWNLOADER $URL
        mv $FILE $THIRD_PARTY
    fi
    if [[ "$DO_INSTALL" == 'True' ]]; then
        ./third-party-install.sh $FILE
    fi
done
